package com.longrise.android.byjk.plugins.course.freecourse;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.CourseDefaultEntityBeanItem;
import com.longrise.android.byjk.model.CourseDefaultItem;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/11/23.
 * 我的课程的adapter
 */

public class FreeCourseAdapter extends BaseMultiItemQuickAdapter<CourseDefaultEntityBeanItem, BaseViewHolder> {

    private List<CourseDefaultEntityBeanItem> mDefaultItemLists = new ArrayList<>();
    private OnItemClickListener mListener;


    public FreeCourseAdapter(List<CourseDefaultEntityBeanItem> items) {
        super(items);
        addItemType(CourseDefaultItem.TYPE1, R.layout.by_default_course_item);
        addItemType(CourseDefaultItem.TYPE2, R.layout.item_freecourse);
    }


    public void refreshData(List<CourseDefaultEntityBeanItem> items) {
        mDefaultItemLists.clear();
        mDefaultItemLists.addAll(items);
        setNewData(items);
    }

    @Override
    protected void convert(BaseViewHolder helper, CourseDefaultEntityBeanItem defaultItem) {
        if (CourseDefaultEntityBeanItem.TYPE1 == defaultItem.getItemType()) {
            return;  //默认样式不需要处理数据
        }

        final EntityBean item = defaultItem.getEntityBean();

        final View convertView = helper.getConvertView();
        ImageView iv = helper.getView(R.id.item_freecourse_iv);
        final TextView tvTitle = helper.getView(R.id.item_freecourse_tv1);
        TextView tvDate = helper.getView(R.id.item_freecourse_tv2);

        if (item != null) {
            String realpic = item.getString("realpic", "");
            Glide.with(mContext)
                    .load(realpic)
                    .placeholder(mContext.getResources().getDrawable(R.drawable.img03))
                    .crossFade()
                    .into(iv);
            Glide.get(mContext).clearMemory();

            //------标题---------
            String name = item.getString("name", ""); //name
            tvTitle.setText(name == null ? "" : name);


            //有效时间
            String expiretime = item.getString("signupendtime", "");//expiretime
            tvDate.setText(expiretime == null || expiretime.equals("") ? "" : "有效期至\u0020" + expiretime.substring(0, 10));

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onClick(item);
                    }
                }
            });


            //让最后的item 分割线不显示
            int layoutPosition = helper.getLayoutPosition();
            if (layoutPosition == mDefaultItemLists.size()) {
                helper.getView(R.id.item_freecourse_divider2).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.item_freecourse_divider2).setVisibility(View.VISIBLE);
            }


        }

    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }


    public interface OnItemClickListener {
        void onClick(EntityBean bean);
    }
}
